home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / servlets / SnoopServlet.java < prev   
Encoding:
Java Source  |  1997-07-18  |  5.9 KB  |  199 lines

  1. /*
  2.  * 1.17 06/25/97
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. import java.io.*;
  23. import java.util.*;
  24.  
  25. import javax.servlet.*;
  26. import javax.servlet.http.*;
  27.  
  28. import sun.security.x509.*;
  29.  
  30.  
  31. /**
  32.  * Snoop servlet. This servlet simply echos back the request line and
  33.  * headers that were sent by the client, plus any HTTPS information
  34.  * which is accessible.
  35.  *
  36.  * @version     1.17, 06/30/97
  37.  * @author     David Connelly
  38.  */
  39. public
  40. class SnoopServlet extends HttpServlet {
  41.  
  42.  
  43.     public void doPost (HttpServletRequest req, HttpServletResponse res)
  44.     throws ServletException, IOException
  45.     {
  46.         //value chosen to limit denial of service
  47.         if (req.getContentLength() > 8*1024) {  
  48.         res.setContentType("text/html");
  49.             ServletOutputStream out = res.getOutputStream();
  50.         out.println("<html><head><title>Too big</title></head>");
  51.         out.println("<body><h1>Error - content length >8k not ");
  52.         out.println("</h1></body></html>");
  53.         } else {
  54.         doGet(req, res);
  55.         }
  56.     }
  57.     
  58.     public void doGet (HttpServletRequest req, HttpServletResponse res)
  59.     throws ServletException, IOException
  60.     {
  61.     res.setContentType("text/html");
  62.     ServletOutputStream out = res.getOutputStream();
  63.     out.println("<html>");
  64.     out.println("<head><title>Snoop Servlet</title></head>");
  65.     out.println("<body>");
  66.  
  67.     out.println("<h1>Requested URL:</h1>");
  68.     out.println("<pre>");
  69.     out.println (HttpUtils.getRequestURL (req).toString ());
  70.     out.println("</pre>");
  71.  
  72.     Enumeration enum = getServletConfig().getInitParameterNames();
  73.         if (enum != null) {
  74.             boolean first = true;
  75.         while (enum.hasMoreElements()) {
  76.         if (first) {
  77.                 out.println("<h1>Init Parameters</h1>");
  78.                 out.println("<pre>");
  79.             first = false;
  80.                 }
  81.         String param = (String) enum.nextElement();
  82.                 out.println(" "+param+": "+getInitParameter(param));
  83.         }
  84.         out.println("</pre>");
  85.         }
  86.  
  87.     out.println("<h1>Request information:</h1>");
  88.     out.println("<pre>");
  89.     print(out, "Request method", req.getMethod());
  90.     print(out, "Request URI", req.getRequestURI());
  91.     print(out, "Request protocol", req.getProtocol());
  92.     print(out, "Servlet path", req.getServletPath());
  93.     print(out, "Path info", req.getPathInfo());
  94.     print(out, "Path translated", req.getPathTranslated());
  95.     print(out, "Query string", req.getQueryString());
  96.     print(out, "Content length", req.getContentLength());
  97.     print(out, "Content type", req.getContentType());
  98.     print(out, "Server name", req.getServerName());
  99.     print(out, "Server port", req.getServerPort());
  100.     print(out, "Remote user", req.getRemoteUser());
  101.     print(out, "Remote address", req.getRemoteAddr());
  102.     print(out, "Remote host", req.getRemoteHost());
  103.     print(out, "Authorization scheme", req.getAuthType());
  104.     out.println("</pre>");
  105.     
  106.     Enumeration e = req.getHeaderNames();
  107.     if (e.hasMoreElements()) {
  108.         out.println("<h1>Request headers:</h1>");
  109.         out.println("<pre>");
  110.         while (e.hasMoreElements()) {
  111.         String name = (String)e.nextElement();
  112.         out.println(" " + name + ": " + req.getHeader(name));
  113.         }
  114.         out.println("</pre>");
  115.     }
  116.  
  117.     e = req.getParameterNames();
  118.     if (e.hasMoreElements()) {
  119.         out.println("<h1>Servlet parameters (Single Value style):</h1>");
  120.         out.println("<pre>");
  121.         while (e.hasMoreElements()) {
  122.         String name = (String)e.nextElement();
  123.         out.println(" " + name + " = " + req.getParameter(name));
  124.         }
  125.         out.println("</pre>");
  126.     }
  127.  
  128.     e = req.getParameterNames();
  129.     if (e.hasMoreElements()) {
  130.         out.println("<h1>Servlet parameters (Multiple Value style):</h1>");
  131.         out.println("<pre>");
  132.         while (e.hasMoreElements()) {
  133.         String name = (String)e.nextElement();
  134.         String vals[] = (String []) req.getParameterValues(name);
  135.         if (vals != null) {
  136.             out.print("<b> " + name + " = </b>"); 
  137.             out.println(vals[0]);
  138.             for (int i = 1; i<vals.length; i++)
  139.             out.println("           " + vals[i]);
  140.         }
  141.         out.println("<p>");
  142.         }
  143.         out.println("</pre>");
  144.     }
  145.  
  146.     String    cipherSuite = (String)
  147.         req.getAttribute ("javax.net.ssl.cipher_suite");
  148.  
  149.     if (cipherSuite != null) {
  150.         X509Cert    certChain [] = (X509Cert [])
  151.         req.getAttribute ("javax.net.ssl.peer_certificates");
  152.         
  153.         out.println ("<h1>HTTPS Information:</h1>");
  154.         out.println("<pre>");
  155.  
  156.         out.println ("Cipher Suite:  " + cipherSuite);
  157.  
  158.         if (certChain != null) {
  159.         for (int i = 0; i < certChain.length; i++) {
  160.             out.println ("client cert chain [" + i + "] = "
  161.             + certChain [i].toString ());
  162.         }
  163.         }
  164.  
  165.         // javax.net.ssl.session --> ssl.Session object
  166.         // ... has above data plus creation and last used dates
  167.  
  168.         out.println("</pre>");
  169.     }
  170.  
  171.     
  172.     out.println("</body></html>");
  173.     }
  174.  
  175.     private void print(ServletOutputStream out, String name, String value)
  176.     throws IOException
  177.     {
  178.     out.print(" " + name + ": ");
  179.     out.println(value == null ? "<none>" : value);
  180.     }
  181.  
  182.     private void print(ServletOutputStream out, String name, int value)
  183.     throws IOException
  184.     {
  185.     out.print(" " + name + ": ");
  186.     if (value == -1) {
  187.         out.println("<none>");
  188.     } else {
  189.         out.println(value);
  190.     }
  191.     }
  192.  
  193.     private static final String UNKNOWN = "<unknown>";
  194.  
  195.     public String getServletInfo() {
  196.     return "A servlet that shows the request headers sent by the client";
  197.     }
  198. }
  199.